const app = getApp()
Page({
  data: {
    currentTab: "check",
    unTested: true,
    testing: false,
    score: 60,
    scoreColor: '',
    deviceID: null,
    deviceInfo: {},
    infoList: [
      {
        id: '0',
        name: '通道连接稳定性',
        isExpanded: false,
        field: 'tdljwdx',
        result: 1,
        children: [
          {
            id: '0-0',
            name: '设备与通道间链接',
            field: 'sbtdlj',
            result: 1
          },
          {
            id: '0-1',
            name: '通道断流情况',
            field: 'tddl',
            result: 1
          },
          {
            id: '0-2',
            name: '网络链接',
            field: 'wllj',
            result: 1
          }
        ]
      },
      {
        id: '1',
        name: '录像记录完整性',
        isExpanded: false,
        field: 'lxjlwzx',
        result: 1,
        children: [
          {
            id: '1-0',
            name: '采集完整性',
            field: 'cjwzx',
            result: 1
          },
          {
            id: '1-1',
            name: '存储完整性',
            field: 'ccwzx',
            result: 1
          },
          {
            id: '1-2',
            name: '回访验证',
            field: 'hfyz',
            result: 1
          },
          {
            id: '1-3',
            name: '日志与记录检查',
            field: 'rzyjljc',
            result: 1
          }
        ]
      },
      {
        id: '2',
        name: '故障发现及时性',
        isExpanded: false,
        field: 'gzfxjsx',
        result: 1,
        children: [
          {
            id: '2-0',
            name: '预防性维护',
            field: 'yfxwh',
            result: 1
          },
          {
            id: '2-1',
            name: '存储完整性',
            field: 'cjwzx',
            result: 1
          },
          {
            id: '2-2',
            name: '诊断工具辅助',
            field: 'zdgjfz',
            result: 1
          }
        ]
      },
      {
        id: '3',
        name: '设备维护及时性',
        isExpanded: false,
        field: 'sbwhjsx',
        result: 1,
        children: [
          {
            id: '3-0',
            name: '运维工单流程',
            field: 'ywgdlc',
            result: 1
          },
          {
            id: '3-1',
            name: '快速反应与智能监控',
            field: 'ksxyzn',
            result: 1
          },
          {
            id: '3-2',
            name: '可追溯记录',
            field: 'kzsjl',
            result: 1
          }
        ]
      }
    ]
  },
  onLoad(option) {
    if (!option.id) {
      wx.showToast({
        title: '参数错误',
        icon: 'error'
      })
      wx.navigateBack()
    } else {
      this.setData({
        deviceID: option.id
      })
    }
    this.getDeviceInfo()
  },
  onBack() {
    wx.navigateBack();
  },
  getDeviceInfo() {
    const now = Math.floor(Date.now() / 1000);
    app.axios('tsdevice', {
      op: 'ajaxTsdeviceDetail',
      zaddtime: now,
      opfile: 'tsdevice',
      needzd: 1,
      id: this.data.deviceID
    }).then(res => {
      if (res.code) {
        if (res.item.addtime) {
          const timespan = this.calcTime(now - res.item.addtime);
          res.timespan = timespan;
          res.recallcus = this.calcTime(res.recallcus);
        }

        // 更新 infoList 的 result 字段
        const updatedInfoList = this.data.infoList.map(item => {
          // 检查当前项是否匹配 res 中的字段
          if (res.zdresult[item.field] !== undefined) {
            item.result = res.zdresult[item.field];
          }

          // 检查子项是否匹配 res 中的字段
          if (item.children && item.children.length > 0) {
            item.children = item.children.map(child => {
              if (res.zdresult[child.field] !== undefined) {
                child.result = res.zdresult[child.field];
              }
              return child;
            });
          }
          return item;
        });

        this.setData({
          deviceInfo: res,
          infoList: updatedInfoList // 更新后的 infoList
        });
      }
    });
  },
  onTabChange(e) {
    this.setData({
      currentTab: e.target.dataset.tab,
    });
  },
  calcTime(time) {
    let timespan = 0
    if (time) {
      timespan = Math.floor((time) / (60 * 60 * 24))
    }
    return timespan
  },
  startTest() {
    this.setData({
      testing: true,
      unTested: true,
    });

    // 模拟测试过程
    const randomSecond = Math.random() * 8000
    setTimeout(() => {
      const r = Math.random();
      let score;

      if (r < 0.5) {
        score = 80 + Math.floor((r / 0.5) * 20); // 80-99分，概率50%
      } else if (r < 0.75) {
        score = 60 + Math.floor(((r - 0.5) / 0.25) * 20); // 60-79分，概率25%
      } else {
        score = Math.floor(((r - 0.75) / 0.25) * 60); // 0-59分，概率25%
      }
      const scoreColor = this.getScoreColor(score);
      const infoList = this.data.infoList.map(item => ({
        ...item,
        allright: score >= 80
      }));

      this.setData({
        testing: false,
        unTested: false,
        score,
        scoreColor,
        infoList
      });
    }, randomSecond);
  },

  getScoreColor(score) {
    return score >= 80 ? 'green' : score < 60 ? 'red' : 'orange';
  },

  toggleExpand(e) {
    const index = e.currentTarget.dataset.index;
    const key = `infoList[${index}].isExpanded`;
    this.setData({
      [key]: !this.data.infoList[index].isExpanded
    });
  },

  goChannelPage() {
    wx.navigateTo({
      url: '/pages/client/channel_list/index',
    });
  }
});
